package ru.alt22.visor.php.ui.actions;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.window.Window;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.swt.custom.BusyIndicator;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.osgi.framework.Bundle;

import ru.alt22.visor.php.core.PDToolsCorePlugin;
import ru.alt22.visor.php.ui.PDToolsUIPlugin;
import ru.alt22.visor.php.ui.core.wizards.NewTypeWizard;

public class NewTypeWizardAction extends AbstractDelegatedAction {

	private final static String ATT_NAME = "name";//$NON-NLS-1$
	private final static String ATT_CLASS = "class";//$NON-NLS-1$
	private final static String ATT_ICON = "icon";//$NON-NLS-1$
	private static final String TAG_DESCRIPTION = "description";	//$NON-NLS-1$

	private IConfigurationElement configurationElement;
	private Shell shell;
	protected NewTypeWizard wizard;

	public NewTypeWizardAction() {
		this(null);
	}

	public NewTypeWizardAction(IConfigurationElement element) {
		super();

		shell = null;
		configurationElement = element;

		if (element != null) {
			setText(configurationElement.getAttribute(ATT_NAME));

			String description = getDescriptionFromConfig(configurationElement);
			setDescription(description);
			setToolTipText(description);
			setImageDescriptor(getIconFromConfig(configurationElement));
		}
	}

	private String getDescriptionFromConfig(IConfigurationElement config) {
		IConfigurationElement [] children = config.getChildren(TAG_DESCRIPTION);
		if (children.length>=1) {
			return children[0].getValue();
		}
		return ""; //$NON-NLS-1$
	}

	private ImageDescriptor getIconFromConfig(IConfigurationElement config) {
		String iconName = config.getAttribute(ATT_ICON);
		if (iconName != null) {
			return PDToolsUIPlugin.getImageDescriptor(iconName);
		}
		return null;
	}

	public void run() {
		try {
			createWizard();
		} catch (CoreException e) {
			return;
		}
		try {
			wizard.init(PlatformUI.getWorkbench(), getSelection());
			WizardDialog dialog = new WizardDialog(getShell(), wizard);
			dialog.create();
			int res = dialog.open();

			notifyResult(res == Window.OK);
		} catch (Exception e) {
			PDToolsCorePlugin.log(e);
		}
	}

	public Shell getShell() {
		return shell;
	}

	public void setShell(Shell shell) {
		this.shell = shell;
	}

	protected void createWizard() throws CoreException {
		String pluginId = configurationElement.getContributor().getName();
		Bundle bundle = Platform.getBundle(pluginId);
		if (bundle != null && bundle.getState() == Bundle.ACTIVE ) {
			wizard = (NewTypeWizard) configurationElement.createExecutableExtension(ATT_CLASS);
			return;
		} else {
			final Object[] ret = new Object[1];
			final CoreException[] exc = new CoreException[1];
			BusyIndicator.showWhile(null, new Runnable() {
				public void run() {
					try {
						ret[0] = configurationElement.createExecutableExtension(ATT_CLASS);
					} catch (CoreException e) {
						exc[0] = e;
					}
				}
			});
			if (exc[0] != null) {
				throw exc[0];
			} else {
				wizard = (NewTypeWizard) ret[0];
			}
		}
	}

	public void dispose() {
	}

	public void init(IWorkbenchWindow window) {
	}

}